package com.apes.project.jekunAuto.timeTask.hrTransfer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.event.Listener;
import com.apes.pi.repository.TimestampRepository;
import com.apes.pi.service.PiManager;
import com.apes.project.jekunAuto.report.service.ReportService;
import com.apes.project.jekunAuto.timeTask.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;


@Service("hrTransferSynService")
public class HrTransferSynService extends BaseService {

    @Autowired
    ReportService reportService;
    @Autowired
    private TimestampRepository timestampRepository;


    /*
     *   同步老系统 人员列表
     * */
    @Listener(topic = "event:hr.hrTransferSyn.savePersonInfo")
    public Boolean savePersonInfo(SimpleRequest request) {
        JSONArray synPersonList = request.getJA();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("personList", synPersonList);
        return setData("ipmHrService.updatePersonList", jsonObject);
    }

    /*
     *   同步老系统 奖惩记录
     * */
    @Listener(topic = "event:hr.hrTransferSyn.synRewardPunishByOld")
    public JSONArray synRewardPunishByOld(SimpleRequest request) throws Exception {
        String SynInterface = (String) request.getData();
        LocalDate yDate = LocalDate.now().minusDays(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("SUMDATE",LocalDate.of(2019,7,24));
        jsonObject.put("SUMDATE", formatter.format(LocalDateTime.of(yDate, LocalTime.now())));
        jsonObject.put("ClassName", "ipmJDKHDao");
        jsonObject.put("MethodName", SynInterface);
        return getData("ipmJDKHService.getJdkhData", jsonObject);
    }

    @Listener(topic = "event:hr.hrTransferSyn.saveSupList")
    public Boolean saveSupList(SimpleRequest request) throws Exception {
        Map synSupList = request.getMap();
        JSONObject resultObj = null;
        Boolean flag = true;
        resultObj = JSONObject.parseObject(reportService.sendDataToApp(synSupList, "v1/employee/sync-support-order"));
        if (!resultObj.getBoolean("success")) {
            flag = false;
        }
        return flag;
    }


    /*
     *   新员工 同步到老系统 人员列表
     * */
    @Listener(topic = "event:hr.hrTransferSyn.saveEmployee")
    public Boolean saveEmployee(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmHrService.saveEmployee", jsonObject);
    }


    /**
     * 同步 操作员
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:hr.hrTransferSyn.synUser")
    public Boolean synUser(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmHrService.synUser", jsonObject);
    }

    /**
     * 同步老系统 员工信息 定时 10分钟
     * @param simpleRequest simpleRequest
     */
    public void synEmployeeList(SimpleRequest simpleRequest) {
        // 修改原因，由于 apes-misc 包引入了 hr 模块的包，导致 询价平台 初始化了 HR 模块的表
        sendSynEvent("event:hr.hrTransferSyn.synEmployeeList", new HashMap<>());
    }

    @Listener(topic = "event:hr.hrTransferSyn.checkZGZT")
    public String checkZGZT(SimpleRequest request) {
        String sOnDutyState = (String) request.getData();
        String sYGDAH = "";
        //  在岗状态:(0:试用期;1:代理期;2:待岗期;3:实习期;4:转正;5:长假;9:支援期)
        if (sOnDutyState.equals("000116")) {
            sYGDAH = "0";
        } else if (sOnDutyState.equals("000117")) {
            sYGDAH = "1";
        } else if (sOnDutyState.equals("000118")) {
            sYGDAH = "2";
        } else if (sOnDutyState.equals("000119")) {
            sYGDAH = "3";
        } else if (sOnDutyState.equals("000120")) {
            sYGDAH = "4";
        } else if (sOnDutyState.equals("000393")) {
            sYGDAH = "9";
        } else if (sOnDutyState.equals("000394")) {
            sYGDAH = "5";
        }
        return sYGDAH;
    }


    /**
     * 同步 公司
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synCompany")
    public Boolean synCompany(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertGSXX", jsonObject);
    }

    /**
     * 同步 采购组织
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synCGZZ")
    public Boolean synCGZZ(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertCGZZ", jsonObject);
    }

    /**
     * 同步 采购组织
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synXSZZ")
    public Boolean synXSZZ(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertXSZZ", jsonObject);
    }

    /**
     * 同步 供应商
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synSupplier")
    public Boolean synSupplier(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertGYSXX", jsonObject);
    }

    /**
     * 同步 采购组织分配供应商信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synCGZZGYS")
    public Boolean synCGZZGYS(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertCGZZGYS", jsonObject);
    }

    /**
     * 同步 供应商注册信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synGYSZCXX")
    public Boolean synGYSZCXX(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertGYSZCXX", jsonObject);
    }

    /**
     * 同步 供应商注册信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synGYSCWXX")
    public Boolean synGYSCWXX(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertGYSCWXX", jsonObject);
    }

    /**
     * 同步 单位客户
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synCustomer")
    public Boolean synCustomer(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertKHXX", jsonObject);
    }

    /**
     * 同步 客户财务信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synKHCWXX")
    public Boolean synKHCWXX(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertKHCWXX", jsonObject);
    }

    /**
     * 同步 单位客户注册信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synKHZCXX")
    public Boolean synKHZCXX(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertKHZCXX", jsonObject);
    }


    /**
     * 同步 内部交易对照表
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synNBJYDZB")
    public Boolean synNBJYDZB(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertNBJYDZB", jsonObject);
    }

    /**
     * 同步 部门信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synBMXX")
    public Boolean synBMXX(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertBMXX", jsonObject);
    }

    /**
     * 同步 地点信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synDDXX")
    public Boolean synDDXX(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertDDXX", jsonObject);
    }

    /**
     * 同步 门店信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synMDXX")
    public Boolean synMDXX(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertMDXX", jsonObject);
    }

    /**
     * 同步 门店扩展信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synMDKZXX")
    public Boolean synMDKZXX(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertMDKZXX", jsonObject);
    }


    /**
     * 同步 维护仓库扩展信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synCKXX")
    public Boolean synCKXX(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertCKXX", jsonObject);
    }

    /**
     * 同步 仓库库区信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synCKKQ")
    public Boolean synCKKQ(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertCKKQ", jsonObject);
    }


    /**
     * 查询需要同步的本地权限是否存在
     */
    @Listener(topic = "event:cm.branchEntry.synBDQX")
    public Boolean synBDQX(SimpleRequest request) throws Exception {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertBDQX", jsonObject);

    }


    /**
     * 查询需要同步的本地权限是否存在
     */
    @Listener(topic = "event:cm.branchEntry.synBDQX")
    public Boolean synBDQXMX(SimpleRequest request) throws Exception {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertBDQXMX", jsonObject);

    }


    /**
     * 同步 成本中心信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synCBZX")
    public Boolean synCBZX(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertCBZX", jsonObject);
    }

    /**
     * 同步 利润中心信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synLRZX")
    public Boolean synLRZX(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertLRZX", jsonObject);
    }

    /**
     * 同步 地点配置责任中心信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synDDZRZX")
    public Boolean synDDZRZX(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertDDZRZX", jsonObject);
    }

    /**
     * 同步 采购范围信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synCGFW")
    public Boolean synCGFW(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertCGFW", jsonObject);
    }

    /**
     * 同步 采购组织商品组信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synCGSPZ")
    public Boolean synCGSPZ(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertCGSPZ", jsonObject);
    }

    /**
     * 同步 DC配送范围信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synDCPSFW")
    public Boolean synDCPSFW(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("ipmVipService.insertDCPSFW", jsonObject);
    }

    /**
     * 查询来自旧框架生成的运单的商品列表
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:tms.waybill.findWaybillByZCDD")
    public JSONArray findWaybillByZCDD(SimpleRequest request) throws Exception {
        JSONObject jsonObject = request.getJO();
        return getData("scmZcddService.qryZCDDMXInfo", jsonObject);
    }


    /**
     * 同步 DC配送范围信息
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synFNKCTBFW")
    public Boolean synFNKCTBFW(SimpleRequest request) {
        JSONObject jsonObject = request.getJO();
        return setData("fnkcfwService.insert", jsonObject);
    }

    /**
     * 开店申请推送消息至老系统
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.branchEntry.synUnifiedExport")
    public JSONObject synUnifiedExport(SimpleRequest request) {
        JSONObject body = request.getJO();
        return SpringManager.getBean(PiManager.class).synErp("ipmVipService.insertUnifiedExport", body);
    }

    /**
     * 公司入驻推送消息至老系统
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.companyEntry.synUnifiedExport")
    public JSONObject synCompanyEntryUnifiedExport(SimpleRequest request) {
        JSONObject body = request.getJO();
        return SpringManager.getBean(PiManager.class).synErp("ipmVipService.insertCompanyEntryUnifiedExport", body);
    }


    /**
     * 采购组同步到旧系统
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.purchasingGroup.synCGZ")
    public JSONObject synCGZ(SimpleRequest request) {
        JSONObject body = request.getJO();
        return SpringManager.getBean(PiManager.class).synErp("ipmVipService.insertCGZ", body);
    }

    /**
     * 删除采购组同步到旧系统
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.purchasingGroup.synCGZByDelete")
    public JSONObject synCGZByDelete(SimpleRequest request) {
        JSONObject body = request.getJO();
        return SpringManager.getBean(PiManager.class).synErp("ipmVipService.deleteCGZ", body);
    }

    /**
     * 创建单位客户同步到旧系统
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.customer.synUnifiedExport")
    public JSONObject synCustomUnifiedExport(SimpleRequest request) {
        JSONObject body = request.getJO();
        return SpringManager.getBean(PiManager.class).synErp("ipmVipService.insertCustomerUnifiedExport", body);
    }

    /**
     * 创建供应商同步到旧系统
     *
     * @param request
     * @return
     */
    @Listener(topic = "event:cm.supplier.synUnifiedExport")
    public JSONObject synSupplierUnifiedExport(SimpleRequest request) {
        JSONObject body = request.getJO();
        return SpringManager.getBean(PiManager.class).synErp("ipmVipService.insertSupplierUnifiedExport", body);
    }
}
